package com.pengcheng.spring.bean;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Lookup;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.MessageSource;
import org.springframework.context.MessageSourceAware;
import org.springframework.stereotype.Component;

/**
 * Aware接口，帮我们装配Spring底层的一些组件
 * 1、Bean的功能增强全部都是由 BeanPostProcessor + InitializingBean (合起来完成的)
 * 2、骚操作就是 BeanPostProcessor + InitializingBean
 *
 * Person 为什么能把 ApplicationContext、MessageSource 当作自己的参数传进来。
 * 	- 通过实现接口的方式自动注入了 ApplicationContext、MessageSource。是由 BeanPostProcessor 完成的。
 */
//@Component
public class Person implements ApplicationContextAware, MessageSourceAware {

//	@Autowired
	ApplicationContext context; // 可以要到ioc容器

	MessageSource messageSource;

	private String name;

	private Cat cat;

	public Person() {
		System.out.println("Person 对象创建");
	}

	public ApplicationContext getContext() {
		return context;
	}

	public void setContext(ApplicationContext context) {
		this.context = context;
	}

	@Lookup  //去容器中找。@Bean的这种方式注册的Person @Lookup不生效
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Cat getCat() {
		return cat;
	}

	// @Autowired 使 bena 实现 AutowiredAnnotationBeanPostProcessor 接口
	@Autowired  //去发现一下.....
	public void setCat(Cat cat) {
		this.cat = cat;
	}

	@Override
	public String toString() {
		return "Person{" +
				"context=" + context +
				", name='" + name + '\'' +
				", cat=" + cat +
				'}';
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		// 利用会调机制，把ioc容器传入
		this.context = applicationContext;
	}

	@Override
	public void setMessageSource(MessageSource messageSource) {
		this.messageSource = messageSource;
	}
}
